﻿using AppManager.BLL;
using AppManager.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PushManager.Web
{
    public class BaseDbHub : BaseHub
    {
        public override Task OnConnected()
        {
            var result = GetRequest();
            var isAdmin = result.Item4;
            var deviceStr = result.Item6;

            //必須保證同時只連接了一個admin,否則會導致同一條消息重複發送多次,從數據庫中讀取下,
            //降低pushtask中console的發送頻率,這樣可以減少
            var connect = new Connect
            {
                ConnectionId = Context.ConnectionId,
                DeviceStr = deviceStr,
                IsAdmin = isAdmin,
                IsConnected = true,
            };

            List<string> connectionIds;
            ConnectBLL.AddOrUpdate(connect, out connectionIds);

            //todo 這裡不確定到底要不要斷掉console中的多餘連接是不是會導致console所有的連接都斷掉,這個方法應該不行
            //Clients.Clients(connectionIds).Invoke("Stop"); 

            return base.OnConnected();
        }

        private Tuple<int, uint, bool, bool, byte, string> GetRequest()
        {
            string memberIdStr = Context.Headers.Get("memberId");
            int memberId;
            int.TryParse(memberIdStr, out memberId);

            string deviceStr = Context.Headers.Get("MachineKey");

            var siteIdStr = Context.Headers.Get("siteId");
            uint siteId;
            uint.TryParse(siteIdStr, out siteId);

            string isUseGoogleStr = Context.Headers.Get("IsUseGoogle");
            bool isUseGoogle = false;
            if (!string.IsNullOrEmpty(isUseGoogleStr))
            {
                bool.TryParse(isUseGoogleStr, out isUseGoogle);
            }

            string isAdminStr = Context.Headers.Get("IsAdmin");
            bool isAdmin = false;
            if (!string.IsNullOrEmpty(isAdminStr))
            {
                bool.TryParse(isAdminStr, out isAdmin);
            }

            string langIdStr = Context.Headers.Get("LangId");
            byte langId;
            byte.TryParse(langIdStr, out langId);

            return new Tuple<int, uint, bool, bool, byte, string>(memberId, siteId, isUseGoogle, isAdmin, langId, deviceStr);
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            ConnectBLL.UpdateIsConnect(Context.ConnectionId);

            return base.OnDisconnected(stopCalled);
        }

        public override Task OnReconnected()
        {
            var result = GetRequest();
            var isAdmin = result.Item4;
            var deviceStr = result.Item6;


            var connect = new Connect
            {
                ConnectionId = Context.ConnectionId,
                DeviceStr = deviceStr,
                IsAdmin = isAdmin,
                IsConnected = true,
            };

            List<string> connectionIds;
            ConnectBLL.AddOrUpdate(connect, out connectionIds);
            Clients.Clients(connectionIds).Invoke("Stop");

            return base.OnConnected();
        }
    }
}